package com.gdth.broadcastreceiver;

import java.lang.ref.WeakReference;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.gdth.service.PhoneCallState;
import com.gdth.service.PhoneMediaRecorder;

public class PhoneReceiver extends BroadcastReceiver{
	/**
	 * 弱引用的录音程序
	 */
	private WeakReference<PhoneMediaRecorder> mRecorder;
	
	private LocalBroadcastManager mLocalBroadcastManager;
	
	public PhoneReceiver(WeakReference<PhoneMediaRecorder> mRecorder, LocalBroadcastManager mLocalBroadcastManager){
		this.mRecorder = mRecorder;
		
		this.mLocalBroadcastManager = mLocalBroadcastManager;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String phoneState = intent.getAction();    
         
        if (phoneState.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {    
            String phoneNum = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            PhoneMediaRecorder mPhoneMediaRecorder = mRecorder.get();
            mPhoneMediaRecorder.setNumber(phoneNum);    
            mPhoneMediaRecorder.setIsCommingNumber(false);       
        }    
            
        if (phoneState.equals(PhoneCallState.DIALING)) {
        }    
            
        if (phoneState.equals(PhoneCallState.ALERTING)) {
        	PhoneMediaRecorder mPhoneMediaRecorder = mRecorder.get();
        	mPhoneMediaRecorder.setIsCommingNumber(true); 
        }    
            
        if (phoneState.equals(PhoneCallState.ACTIVE)) {
        	PhoneMediaRecorder mPhoneMediaRecorder = mRecorder.get();
            if (mPhoneMediaRecorder != null && !mPhoneMediaRecorder.isStarted()) {     
            	mPhoneMediaRecorder.start();
            }    
        }    
            
        if (phoneState.equals(PhoneCallState.DISCONNECTED)) {  
        	PhoneMediaRecorder mPhoneMediaRecorder = mRecorder.get();
    
            if (mPhoneMediaRecorder != null && mPhoneMediaRecorder.isStarted()) {    //!recorder.isCommingNumber() && recorder.isStarted()
            	Intent intents = new Intent(PhoneCallState.END);
            	intents.putExtra("uuid", mPhoneMediaRecorder.getUuid());
            	intents.putExtra("number", mPhoneMediaRecorder.getNumber());
            	intents.putExtra("jcuuid", mPhoneMediaRecorder.getJcuuid());
            	intents.putExtra("jtuuid", mPhoneMediaRecorder.getJtuuid());
            	mLocalBroadcastManager.sendBroadcast(intents);
            	//LogUtils.e("PhoneReceiver", "电话录音结束,发出广播" + mPhoneMediaRecorder.getNumber());
            	mPhoneMediaRecorder.stop();
            }    
        }
        
        if (phoneState.equals(PhoneCallState.START)){
        	String number = intent.getStringExtra("number");
        	String wybs = intent.getStringExtra("wybs");
        	String name = intent.getStringExtra("name");
        	startPhone(number, wybs, name);
        }
        
        if (phoneState.equals(PhoneCallState.MEDIA_STAER)){
        	String uuid = intent.getStringExtra("uuid");
        	startMusic(uuid);
        }
        
        if (phoneState.equals(PhoneCallState.MEDIA_END)){
        	stopMusic();
        }
	}
	
	 private void startMusic(String uuid){
//	    	mMdieaPlayr.setMusic(uuid);
//	    	mediaPlayer.startMusic();
	    }
	    
	    private void stopMusic(){
//	    	mMdieaPlayr.stopMusic();
	    }
	
	private void startPhone(String number, String jcuuid, String jtuuid){
    	PhoneMediaRecorder mPhoneMediaRecorder = new PhoneMediaRecorder();
    	mPhoneMediaRecorder.setNumber(number);
    	mPhoneMediaRecorder.setJcuuid(jcuuid);
    	mPhoneMediaRecorder.setJtuuid(jtuuid);
    	
    	mRecorder = new WeakReference<PhoneMediaRecorder>(mPhoneMediaRecorder);

    	//通知页面录音程序已准备
    	sendBroadcastByIntent(PhoneCallState.READY);
    }
	
	/**
     * 发生本地广播
     * @param action
     */
    private void sendBroadcastByIntent(String action){
    	Intent intent = new Intent(action);
    	sendBroadcastByIntent(intent);
    }
    
    /**
     * 发生本地广播
     * @param intent
     */
    private void sendBroadcastByIntent(Intent intent){
    	mLocalBroadcastManager.sendBroadcast(intent);
    }

}
